package client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class clientUDP {
	DatagramSocket clientSocket;
    InetAddress IPAddress;
    DatagramPacket sendPacket;
    DatagramPacket receivePacket;
    byte[] receiveData = new byte[1024];
    public clientUDP(String host){
        try {
            clientSocket= new DatagramSocket();
            IPAddress=InetAddress.getByName("localhost");
            
            receivePacket = new DatagramPacket(receiveData, receiveData.length);
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        } catch (SocketException ex) {
            ex.printStackTrace();
        }
    }
    public void sendData(String st){
        
        try {
        	sendPacket = new DatagramPacket(st.getBytes(), st.getBytes().length, IPAddress, 9876);
            clientSocket.send(sendPacket);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public String getData(){
        
        try {
            
            clientSocket.receive(receivePacket);
            return new String(receivePacket.getData()).trim();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
